package com.xinwei.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.github.pagehelper.PageInfo;
import com.xinwei.common.ApiRestResponse;
import com.xinwei.excpetion.ManagerExceptionEnmu;
import com.xinwei.request.*;
import com.xinwei.service.StudentService;
import com.xinwei.utils.ExcelUtil;
import com.xinwei.utils.StringUtils;
import com.xinwei.vo.StudentVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/api/student")
@CrossOrigin(origins = "*")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/importExcel")
    public void importExcel(@RequestBody ImportExcelReq importExcelReq, HttpServletResponse response){
        String[] title = {"姓名", "学号", "年级", "专业班级", "性别", "民族", "身份证号",
                "宿舍号", "经济情况", "家庭地址", "手机号", "心理关注学生", "按时安康打卡",
                "青年学习", "学费", "旷课情况", "违规违纪情况", "紧急联系方式",
                "备注", "入党情况", "学生干部情况", "挂科情况", "综测排名", "奖学金情况"};

        String[][] content = new String[1][25];
        content[0][0] = importExcelReq.getUserName();
        content[0][1] = importExcelReq.getSchoolNumber();
        content[0][2] = importExcelReq.getGrade();
        content[0][3] = importExcelReq.getMajorAndClass();
        content[0][4] = importExcelReq.getGender();
        content[0][5] = importExcelReq.getNationality();
        content[0][6] = importExcelReq.getIDNumber();
        content[0][7] = importExcelReq.getDormitoryNumber();
        content[0][8] = importExcelReq.getEconomicSituation();
        content[0][9] = importExcelReq.getFamilyAddress();
        content[0][10] = importExcelReq.getPhoneNumber();
        content[0][11] = importExcelReq.getPsychologicalAttention();
        content[0][12] = importExcelReq.getIsClockIn();
        content[0][13] = importExcelReq.getYouthLearning();
        content[0][14] = importExcelReq.getTuitionFee();
        content[0][15] = importExcelReq.getAbsenteeism();
        content[0][16] = importExcelReq.getViolationsOfRegulationsAndDisciplines();
        content[0][17] = importExcelReq.getEmergencyContact();
        content[0][18] = importExcelReq.getRemark();
        content[0][19] = importExcelReq.getPartyCondition();
        content[0][20] = importExcelReq.getStudentLeaders();
        content[0][21] = importExcelReq.getDepartmentStatus();
        content[0][22] = importExcelReq.getOverallRanking();
        content[0][23] = importExcelReq.getScholarShipSituation();

        HSSFWorkbook wb = ExcelUtil.getHSSFWOrkbook("学生信息表", title, content);

        try {
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String("学生信息表.xls".getBytes(), "ISO8859-1"));
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");

            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/studentList")
    public ApiRestResponse getStudentList(@RequestParam Integer classId,
                                          @RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize){
        PageInfo<StudentVO> pageInfo = studentService.getStudentList(classId, pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @PostMapping("/searchStudent")
    public ApiRestResponse searchStudent(@RequestParam String keyWord,
                                         @RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize){
        if (StringUtils.isEmpty(keyWord)){
            return ApiRestResponse.error(ManagerExceptionEnmu.NEED_KEYWORD);
        }
        List<StudentVO> studentVOS = studentService.searchStudent(keyWord, pageNum, pageSize);
        return ApiRestResponse.success(studentVOS);
    }

    @PostMapping("/basicInfo")
    public ApiRestResponse getBasicInfo(@RequestParam Integer studentId){
        return ApiRestResponse.success(studentService.getBasicInfo(studentId));
    }

    @PostMapping("/editBasicInfo")
    public ApiRestResponse editBasicInfo(@RequestBody BasicInfoReq basicInfoReq){
        studentService.editBasicInfo(basicInfoReq);
        return getBasicInfo(basicInfoReq.getStudentId());
    }

    @PostMapping("/dailyManagerment")
    public ApiRestResponse getDailyManagerment(@RequestParam Integer studentId){
        return ApiRestResponse.success(studentService.getDailyManagerment(studentId));
    }

    @PostMapping("/editDailyManagerment")
    public ApiRestResponse editDailyManagerment(@RequestBody DailyManagermentReq dailyManagermentReq){
        studentService.editDailyManagerment(dailyManagermentReq);
        return getDailyManagerment(dailyManagermentReq.getStudentId());
    }

    @PostMapping("/political")
    public ApiRestResponse getPolitical(@RequestParam Integer studentId){
        return ApiRestResponse.success(studentService.getPolitical(studentId));
    }

    @PostMapping("/editPolitical")
    public ApiRestResponse editPolitical(@RequestBody PoliticalReq politicalReq){
        studentService.editPolitical(politicalReq);
        return getPolitical(politicalReq.getStudentId());
    }

    @PostMapping("/academicSituation")
    public ApiRestResponse getAcademicSituation(@RequestParam Integer studentId){
        return ApiRestResponse.success(studentService.getAcademicSituation(studentId));
    }

    @PostMapping("/editAcademicSituation")
    public ApiRestResponse editAcademicSituation(@RequestBody AcademicSituationReq academicSituationReq){
        studentService.editAcademicSituation(academicSituationReq);
        return getAcademicSituation(academicSituationReq.getStudentId());
    }

    @GetMapping("/getPoint")
    public ApiRestResponse getPoint(Integer studentId, Integer term){
        return ApiRestResponse.success(studentService.getPoint(studentId, term));
    }
}
